﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 游戏平台原型
{
    public class Node
    {
        public long no;//编号
        public Node next; //引用域,即下一结点

        //构造器：数据域+引用域，普通结点

        public Node(long newno)
        {
            no = newno;
            next = null;
        }
        public Node()
        {
            next = null;
        }
    }
    public class Linklist
    {
        private Node head; //单链表的头结点

        public Linklist()
        {
            head = null;
        }
        public long ListLength()
        {
            Node p = head;
            long len = 0;
            while (p != null)
            {
                ++len;
                p = p.next;
            }
            return len;
        }

        //清空单链表
        public void Clear()
        {
            head = null;
        }

        //判断单链表是否为空
        public bool ListEmpty()
        {
            if (head == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //在单链表的末尾添加新元素
        public bool ListInsert(long no)
        {
            Node q = new Node(no);
            Node p = new Node();
            if (head == null)
            {
                head = q;
                return true;
            }
            p = head;
            while (p.next != null)
            {
                p = p.next;
            }
            p.next = q;
            return true;
        }
        public void ListDelete(long no)
        {
            long i = FindFirstElem(no);
            if (ListEmpty() || i < 0 || i > ListLength())
            {
                //Console.WriteLine("LinkList is empty or Position is error!");
                return;
            }
            Node q = new Node();
            if (i == 1)
            {
                q = head;
                head = head.next;
            }
            Node p = head;
            long j = 1;
            while (p != null && j < i)
            {
                ++j;
                q = p;
                p = p.next;
            }
            if (j == i)
            {
                if (p != null)
                    q.next = p.next;
            }
            else
            {
                //Console.WriteLine("The " + i + "th node is not exist!");
                return;
            }
        }
        //在单链表中查找值为value的结点
        public long FindFirstElem(long value)
        {
            if (ListEmpty())
            {
                //Console.WriteLine("LinkList is Empty!");
                return -1;
            }
            Node p = new Node();
            p = head;
            long i = 1;
            while (p != null)
            {
                if (p.no.Equals(value))
                    break;
                p = p.next;
                ++i;
            }
            if (p == null)
                return -2;
            else
                return i;
        }

        //显示链表
        public void DispList()
        {
            Node p = new Node();
            p = head;
            while (p != null)
            {
                Console.WriteLine(p.no);
                p = p.next;
            }
        }
    }
}
